package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.SaleDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleService;
import com.atguigu.jxc.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dongdong lau
 * @CreateTime: 2021/7/25 12:52
 * @Description: 销售管理接口实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SaleServiceImpl implements SaleService {

    @Autowired
    private SaleDao saleDao;

    @Autowired
    private UserDao userDao;

    /**
     * 新增销售出库表
     * @param saleList
     * @param saleListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(SaleList saleList, String saleListGoodsStr) {
        // 请求参数进行判空
        if(saleList == null || StringUtil.isEmpty(saleListGoodsStr)){
            return new ServiceVO(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        //查询当前登录用户
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        // 为saleList设置userId属性
        saleList.setUserId(user.getUserId());
        // 将saleList进行保存
        saleDao.saveSaleList(saleList);
        // 将saleListGoodsStr转换成List数组
        List<SaleListGoods> saleListGoodsList = JSON.parseArray(saleListGoodsStr, SaleListGoods.class);
        // 循环遍历进行保存
        for (SaleListGoods saleListGoods : saleListGoodsList) {
            // 设置saleListGoods的sale_List_id属性
            saleListGoods.setSaleListId(saleList.getSaleListId());
            saleDao.saveSaleListGoods(saleListGoods);
            // 保存saleListGoods之后需要扣减商品库存
            saleDao.reduceGoods(saleListGoods.getGoodsId(), saleListGoods.getGoodsNum());
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 客户商品退货单保存
     * @param customerReturnList
     * @param customerReturnListGoodsStr
     * @return
     */
    @Override
    public ServiceVO customerReturnSave(CustomerReturnList customerReturnList, String customerReturnListGoodsStr) {
        // 请求参数进行判空
        if(customerReturnList == null || StringUtil.isEmpty(customerReturnListGoodsStr)){
            return new ServiceVO(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        //查询当前登录用户
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        // 为customerReturnList设置userId属性
        customerReturnList.setUserId(user.getUserId());
        // 将customerReturnList进行保存
        saleDao.saveCustomerReturnList(customerReturnList);
        // 将customerReturnListGoodsStr转换成数组
        List<CustomerReturnListGoods> customerReturnListGoodsList = JSON.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);
        // 对customerReturnListGoodsList进行遍历保存
        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            // 为customerReturnListGoods设置customer_return_list_id
            customerReturnListGoods.setCustomerReturnListId(customerReturnList.getCustomerReturnListId());
            saleDao.saveCustomerReturnListGoods(customerReturnListGoods);
            // 保存之后需要增加库存
            saleDao.addGoods(customerReturnListGoods.getGoodsId(), customerReturnListGoods.getGoodsNum());
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 销售单列表展示
     * @param saleNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> getSaleList(String saleNumber,
                                           Integer customerId,
                                           Integer state,
                                           String sTime,
                                           String eTime) {
        // 定义返回参数
        Map<String, Object> map = new HashMap<>();
        // 调用接口获取数据
        List<SaleList> saleList = saleDao.getSaleList(saleNumber, customerId, state, sTime, eTime);
        // 填充返回数据
        map.put("rows", saleList);
        // 返回数据
        return map;
    }
}
